package cn.tedu._07weibo.controller;

import cn.tedu._07weibo.base.response.JsonResult;
import cn.tedu._07weibo.base.response.StatusCode;
import cn.tedu._07weibo.mapper.WeiboMapper;
import cn.tedu._07weibo.pojo.dto.WeiboAddParam;
import cn.tedu._07weibo.pojo.entity.Weibo;
import cn.tedu._07weibo.pojo.vo.UserLoginVO;
import cn.tedu._07weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._07weibo.pojo.vo.WeiboListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@Tag(name = "02-微博管理")
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("add")
    @Operation(summary = "发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeibo(@RequestBody WeiboAddParam param, HttpSession session){
        log.debug("输入数据："+param);
        UserLoginVO loginVO=(UserLoginVO) session.getAttribute("user");
//        if (loginVO==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(param,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(loginVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.OK();
    }

    @GetMapping("selectIndex")
    @Operation(summary = "查询首页微博")
    @ApiOperationSupport(order = 20)
    public JsonResult selectWeibo(){

         List<WeiboListVO> list=weiboMapper.selectWeiboList();
         return JsonResult.OK(list);

    }
    @GetMapping("selectweibo")
    @Operation(summary = "查询微博详情")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id",description = "微博id")
    public JsonResult selectWeiboById(@Range(min=100L,max=500L,message = "微博id范围100-500") Long id){
        log.debug("微博id："+id);

        List<WeiboDetailVO> datailVO=weiboMapper.selectWeiboById(id);
        return JsonResult.OK(datailVO);
    }




}
